#include <buola/scene/controllers/cphysicscontroller.h>

#include <buola/gui/cwindow.h>
#include <buola/scene/cscene.h>

namespace buola { namespace scene {

CPhysicsController::CPhysicsController(PScene pScene)
    :   mScene(pScene)
{
    mTimer.sTimer.Connect([&]{OnTimer();});
}
    
void CPhysicsController::OnButtonDown(const gui::CMouseEvent &pE)
{
    if(pE.mButton==gui::EMouseButton::FORWARD)
    {
        mTarget=pE.mTarget;
        OnTimer();
        mTimer.SetInterval(20_ms,true);
    }
}

void CPhysicsController::OnButtonUp(const gui::CMouseEvent &pE)
{
    if(pE.mButton==gui::EMouseButton::FORWARD)
    {
        mTimer.Disable();
        mTarget=nullptr;
    }
}

void CPhysicsController::OnTimer()
{
    if(mTarget)
    {
        mScene->StepPhysics();
        mTarget->Refresh();
    }
}

/*namespace scene*/ } /*namespace buola*/ }
